<?php
/**
 * @desc 海运管理
 * @author kevin wang
 * @date 2014-10-25
 */
class ctrl_ship extends ctrl_base {
    private static $dirName = 'ship';
    
    /**
     * @desc 地区列表
	 * @author kevin wang
	 * @param void
	 * @return void
     */
    public static function main() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('ship_list'));
		//页面输入参数处理
    	$currentPage = isset($_POST['pageNum']) ? $_POST['pageNum'] : 1;
    	$pagesize = isset($_POST['numPerPage']) ? $_POST['numPerPage'] : self::$_pagesize;
		$nextpage = ($currentPage - 1) * $pagesize;

    	$province = isset($_POST['province']) ? trim($_POST['province']) : '';
		$city = isset($_POST['city']) ? trim($_POST['city']) : '';
		$port = isset($_POST['port']) ? trim($_POST['port']) : '';
		$port_dest = isset($_POST['port_dest']) ? trim($_POST['port_dest']) : '';
    	//搜索条件组合
		$condition = array();
    	if(!empty($province)) {
    		$condition['province'] = $province;
    	}
		if(!empty($city)) {
    		$condition['city'] = $city;
    	}
		if(!empty($port)) {
    		$condition['port'] = $port;
    	}
		if(!empty($port_dest)) {
    		$condition['port_dest'] = $port_dest;
    	}
		//订单获取
    	$area_list = model_ship :: get_ship_list($nextpage,$pagesize,$condition);
    	$total = array_pop($area_list);
		//模板赋值
    	self::smarty() -> assign('area_list', $area_list);
		self::smarty() -> assign('total', $total);
		self::smarty() -> assign('pageNum', $currentPage);
		self::smarty() -> assign('numPerPage', $pagesize);
		self::smarty() -> assign('pageNumShown', self::$_pageNumShown);
		self::smarty() -> assign('province', $province);
		self::smarty() -> assign('city', $city);
		self::smarty() -> assign('port', $port);
		self::smarty() -> assign('port_dest', $port_dest);
		self::smarty() -> assign('shoNum', self::$_shoNum);
		
    	self::smarty() -> display(self::$dirName . '/ship_list.html');    	
    }
	//添加地区
	public static function ship_add(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('ship_add'));
		if($_POST){

			$ship = self::get_request_data();
			$ship['author'] = $_SESSION['admin']['username'];
			$ship['editer'] = $_SESSION['admin']['username'];
			if(model_ship::ship_exists($ship['area_id'], $ship['port'], $ship['port_dest'])){
				self::ajax_ouput(300, '添加失败,该地址已存在');
			}
			$result = model_ship::ship_add($ship);
			if(!empty($result)){
				self::ajax_ouput(200, '操作成功','ship','closeCurrent');
			}else{
				self::ajax_ouput(300, '添加失败');
			}
		}
		$province = model_area::get_province_list();
		$city = array(array("area_id"=>"","city"=>"请选择城市"));
		array_unshift($province, array("province"=>"请选择省份"));
		self::smarty() -> assign('province', $province);
		self::smarty() -> assign('city', $city);
		self::smarty() -> display(self::$dirName . '/ship_add.html');
	}
	//删除订单
	public static function ship_delete() {
    	if(!model_admin::islogin()) {
			self::goto_login();
    	}
		self::operation(array('ship_delete'));
    	$ids = $_POST['ids'];
		$ids = explode(",", $ids);
		foreach($ids as $v){
			$v = intval($v);
			$transport = model_ship::get_ship_by_id($v);
			$tmp = model_transporttmp::get_tmp_by_transport(3, $v);
			if(!empty($tmp)){
				self::ajax_ouput(300, "操作失败,海运模板有 {$transport['province']}/{$transport['city']}/{$transport['port']}/{$transport['port_dest']} 的地区关联");
			}
			$id[] = $v;
		}
    	$data = array('delete_flag' => 1);

    	$st = model_ship::update_ship_info($id, $data);

    	if($st > 0) {
			self::ajax_ouput(200, '操作成功','ship');
    	} else {
			self::ajax_ouput(300, '操作失败');
    	}
    }
	//编辑区域
	 public static function ship_edit() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		$id = isset($_GET['id'])? (intval($_GET['id'])):(intval($_POST['id']));
    	if($id <= 0) {
			self::ajax_ouput(300, '非法访问');
    	}
		$ship = model_ship::get_ship_by_id($id);
		$ctrl_tag = 'ship_edit_other';
		if($ship['author'] == $_SESSION['admin']['username']){
			$ctrl_tag = 'ship_edit_self';
		}
		self::operation(array($ctrl_tag));
    	if($_POST){
			$ship = array();

			$ship = self::get_request_data();
			$ship['editer'] = $_SESSION['admin']['username'];
			if(model_ship::ship_exists($ship['area_id'], $ship['port'], $ship['port_dest'], $ship['id'])){
				self::ajax_ouput(300, '修改失败,该地址已存在');
			}
			unset($ship['id']);

			$result = model_ship::update_ship_info(explode(',',$id), $ship);
	    	
		    if(!empty($result)){
				self::ajax_ouput(200, '操作成功','ship','closeCurrent');
			}else{
				self::ajax_ouput(300, '操作失败');
			}    		
    	}
    	self::smarty() -> assign('id', $id);
    	self::smarty() -> assign('ship', $ship);
		$province = model_area::get_province_list();
		$city = model_area::get_city_list($ship['province']);
		array_unshift($province, array("province"=>"请选择省份"));
		array_unshift($city, array("area_id"=>"","city"=>"请选择城市"));
		self::smarty() -> assign('province', $province);
		self::smarty() -> assign('city', $city);
    	self::smarty() -> display(self::$dirName . '/ship_edit.html');
    }
	//批量上传模板下载
	public static function get_betch_model(){
		self::download_xls_output(PATH_WWW.DOWNLOAD_FILES_PATH.'betch_area_ship'.self::$_excel_pre);
	}
	//批量上传
	public static function batch(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('ship_add'));
		//文件上传
		$info = self::file_upload_xls();
		//xls文件解析
		framework_base_autoloader::import_vendor('excel/PHPExcel.php');
		$objPHPExcel = new PHPExcel();
		$objPHPExcel = PHPExcel_IOFactory::load($info['url']);
		$csivit = $objPHPExcel->getSheet(0)->toArray();
		//数据读取
		unset($csivit[0]);
		foreach($csivit as $key => $value){
			$len = count($value);
			$csivit[$key][$len] = $_SESSION['admin']['username'];
			$csivit[$key][$len + 1] = $_SESSION['admin']['username'];
		}
		$result = model_ship::batch_insert_ship($csivit);
		if(!empty($result)){
			self::ajax_ouput(200, '操作成功','ship','closeCurrent');
		}else{
			self::ajax_ouput(300, '操作失败,请重新上传');
		}
	}
}
